package com.seqb.manage.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seqb.common.mybatis.WrapperHandler;
import com.seqb.common.utils.ElasticsearchUtils;
import com.seqb.manage.log.dao.LogDao;
import com.seqb.manage.log.domain.LogDO;
import com.seqb.manage.log.service.LogService;
import com.seqb.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Map;

/**
 * @author lilinshen
 * @title 系统日志管理表
 * @description 请填写相关描述
 * @date 2019/11/14
 */
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogDao logDao;
    @Autowired
    private ElasticsearchUtils elasticsearchUtils;

    @Override
    public LogDO get(Long id) {
        return logDao.selectById(id);
    }

    @Override
    public PageUtils list(Map<String, Object> map) {
        try {
            PageUtils pageUtils = elasticsearchUtils.list(map, LogDO.class);
            if (null != pageUtils) {
                return pageUtils;
            }
            if (null != map && null != map.get("pageNumber") && null != map.get("pageSize")) {
                PageHelper.startPage(Integer.valueOf(map.get("pageNumber").toString()), Integer.valueOf(map.get("pageSize").toString()));
            } else {
                PageHelper.startPage(1, Integer.MAX_VALUE);
            }
            QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, LogDO.class);
            Page<LogDO> page = (Page<LogDO>) logDao.selectList(queryWrapper);
            return new PageUtils(page.getResult(), page.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public int count(Map<String, Object> map) {
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, LogDO.class);
        return logDao.selectCount(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(LogDO log) {
        try {
            int insert = logDao.insert(log);
            if (insert > 0) {
                elasticsearchUtils.save(log);
            }
            return insert;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(LogDO log) {
        try {
            int update = logDao.updateById(log);
            if (update > 0) {
                elasticsearchUtils.update(log);
            }
            return update;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(Long id) {
        try {
            int delete = logDao.deleteById(id);
            if (delete > 0) {
                elasticsearchUtils.deleteById(id, LogDO.class);
            }
            return delete;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchRemove(Long[] ids) {
        try {
            int delete = logDao.deleteBatchIds(Arrays.asList(ids));
            if (delete > 0) {
                for (Long id : ids) {
                    elasticsearchUtils.deleteById(id, LogDO.class);
                }
            }
            return delete;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
